package com.yunji.framework_template.common.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;

/**
 * Http请求的结果
 * 
 * @author ddp1j32
 * @date 2015-2-5
 */
public class HttpResult {

    /**
     * Http请求响应码，如200,400等
     */
    private int                                    code;
    /**
     * Http请求响应的内容
     */
    private String                                 content;
    /**
     * 当前请求处理是否成功
     */
    private boolean                                result;
    /**
     * 有时由于301或302跳转，最终请求的URL会变化，此处的URL是最终请求的的URL
     */
    private String                                 finalUrl;
    /**
     * commons http response header
     */
    private org.apache.commons.httpclient.Header[] responseHeader;
    /**
     * Cookie List
     */
    private CookieStore                            cookieStore;
    /**
     * Cookie Map
     */
    private Map<String, String>                    cookieMap = new HashMap<String, String>();
    /**
     * http response header
     */
    private org.apache.http.Header[]               httpResponseHeader;

    public HttpResult(){

    }

    public HttpResult(int code, String content){
        this.code = code;
        this.content = content;
    }

    public HttpResult(int code, String content, boolean result){
        this.code = code;
        this.content = content;
        this.result = result;
    }

    public HttpResult(int code, String content, boolean result, String finalUrl){
        this.code = code;
        this.content = content;
        this.result = result;
        this.finalUrl = finalUrl;
    }

    public HttpResult(int code, String content, org.apache.commons.httpclient.Header[] responseHeader){
        this.code = code;
        this.content = content;
        this.responseHeader = responseHeader;
    }

    public HttpResult(int code, String content, String finalUrl, org.apache.commons.httpclient.Header[] responseHeader){
        this.code = code;
        this.content = content;
        this.finalUrl = finalUrl;
        this.responseHeader = responseHeader;
    }

    public HttpResult(int code, String content, String finalUrl, org.apache.commons.httpclient.Header[] responseHeader,
                      CookieStore cookieStore){
        this.code = code;
        this.content = content;
        this.finalUrl = finalUrl;
        this.responseHeader = responseHeader;
        this.cookieStore = cookieStore;
        initCookieMap(cookieStore);
    }

    public HttpResult(int code, String content, org.apache.http.Header[] httpResponseHeader){
        this.code = code;
        this.content = content;
        this.httpResponseHeader = httpResponseHeader;
    }
    
    public HttpResult(int code, String content, org.apache.http.Header[] httpResponseHeader,CookieStore cookieStore){
        this.code = code;
        this.content = content;
        this.httpResponseHeader = httpResponseHeader;
        this.cookieStore = cookieStore;
        initCookieMap(cookieStore);
    }

    public HttpResult(int code, String content, String finalUrl, org.apache.http.Header[] httpResponseHeader,
                      CookieStore cookieStore){
        this.code = code;
        this.content = content;
        this.finalUrl = finalUrl;
        this.httpResponseHeader = httpResponseHeader;
        this.cookieStore = cookieStore;
        initCookieMap(cookieStore);
    }

    public HttpResult(int code, org.apache.http.Header[] httpResponseHeader){
        this.code = code;
        this.httpResponseHeader = httpResponseHeader;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getFinalUrl() {
        return finalUrl;
    }

    public void setFinalUrl(String finalUrl) {
        this.finalUrl = finalUrl;
    }

    public org.apache.commons.httpclient.Header[] getResponseHeader() {
        return responseHeader;
    }

    public void setResponseHeader(org.apache.commons.httpclient.Header[] responseHeader) {
        this.responseHeader = responseHeader;
    }

    public org.apache.http.Header[] getHttpResponseHeader() {
        return httpResponseHeader;
    }

    public void setHttpResponseHeader(org.apache.http.Header[] httpResponseHeader) {
        this.httpResponseHeader = httpResponseHeader;
    }

    public CookieStore getCookieStore() {
        return cookieStore;
    }

    public void setCookieStore(CookieStore cookieStore) {
        this.cookieStore = cookieStore;
    }

    /**
     * 根据Cookie名称，获取单个Cookie的值
     * 
     * @param cookieName
     * @return
     */
    public String getOneCookie(String cookieName) {
        return cookieMap.get(cookieName);
    }

    private void initCookieMap(CookieStore cookieStore) {
        if (cookieStore != null) {
            List<Cookie> cookieList = cookieStore.getCookies();
            if (cookieList != null && cookieList.size() > 0) {
                cookieList.forEach(c -> {
                    cookieMap.put(c.getName(), c.getValue());
                });
            }
        }
    }

    public String toString() {
        return new StringBuilder("code:").append(code).append(",finalUrl:").append(finalUrl).append(",result:").append(result).append(",content:").append(content).append(",responseHeader:").append(responseHeader).append(",httpResponseHeader:").append(httpResponseHeader).toString();
    }
}
